package com.night.sqlserver.schedule;

import com.night.sqlserver.mapper.source.SourceMapper;
import com.night.sqlserver.mapper.target.TargetMapper;
import com.night.sqlserver.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling // 启用定时任务
public class TransferSchedule {

    @Autowired
    private SourceMapper sourceMapper;
    @Autowired
    private TargetMapper targetMapper;
    @Autowired
    private DataService dataService;

    //@Scheduled(cron = "0/3 * * * * ?") // 每3秒执行一次
    public void scheduler() {
        System.out.println("=====开始时间：" + LocalDateTime.now());
        List<HashMap> tableList = sourceMapper.findAllTable();
        if (tableList.size() > 0) {
            for (HashMap map : tableList) {
                String tableName = map.get("name").toString();
                Map sourceMap = new HashMap();
                sourceMap.put("tableName", tableName);
                List<HashMap> pkList = targetMapper.findPrimaryKey(sourceMap);
                if (pkList.size() > 0) {
                    int maxId = Integer.parseInt(pkList.get(0).get("MAX_ID").toString());
                    if (maxId == 0) {
                        List<HashMap> maxIdList = sourceMapper.findMaxId(sourceMap);
                        if (maxIdList.size() > 0) {
                            int id = Integer.parseInt(maxIdList.get(0).get("id").toString());
                            int start = id - 100;
                            dataService.batchByEntity(tableName, start, id);
                            id = start;
                            Map _map = new HashMap();
                            _map.put("maxId", id);
                            _map.put("tableName", tableName);
                            targetMapper.insertMaxId(_map);
                        }
                    } else if (maxId > 0) {
                        int start = maxId - 100;
                        dataService.batchByEntity(tableName, start, maxId);
                        Map _map = new HashMap();
                        _map.put("maxId", start);
                        _map.put("tableName", tableName);
                        targetMapper.insertMaxId(_map);
                    }
                }


            }
        }

        System.out.println("=====结束时间：" + LocalDateTime.now());

    }

    @Scheduled(cron = "0/3 * * * * ?") //每3秒执行一次
    public void transferByEntity() {
        System.out.println("=====开始时间：" + LocalDateTime.now());
        List<HashMap> tableList = sourceMapper.findAllTable();
        if (tableList.size() > 0) {
            for (HashMap map : tableList) {
                String tableName = map.get("name").toString();
                Map sourceMap = new HashMap();
                sourceMap.put("tableName", tableName);
                List<HashMap> pkList = targetMapper.findPrimaryKey(sourceMap);
                if (pkList.size() > 0) {
                    int endId = Integer.parseInt(pkList.get(0).get("END_ID").toString());
                    if (endId > 0) {
                        int start = endId - 500;
                        Map _map = new HashMap();
                        _map.put("endId", start);
                        _map.put("tableName", tableName);
                        targetMapper.insertMaxId(_map);
                        dataService.batchByEntity(tableName, start, endId);
                    } else {
                        List<HashMap> maxIdList = sourceMapper.findMaxId(sourceMap);
                        if (maxIdList.size() > 0) {
                            endId = Integer.parseInt(maxIdList.get(0).get("id").toString());
                            int start = endId - 500;
                            Map _map = new HashMap();
                            _map.put("endId", start);
                            _map.put("tableName", tableName);
                            targetMapper.insertMaxId(_map);
                            dataService.batchByEntity(tableName, start, endId);
                        }
                    }
                }


            }
        }

        System.out.println("=====结束时间：" + LocalDateTime.now());
    }
}
